using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Configuration;
using System.Collections.Specialized;
using System.Web.Caching;

namespace BCGC.BLL.Helper
{
    public class CustomSiteMap
    {
        #region Direct Link Access Properties

        /* *******************************************
         * There needs to be a property for each page,
         * it is strongly recommanded to navigate pages
         * this way and there should not be any hard
         * coded url in the whole application.
         * *******************************************/

        public static string Home
        {
            get { return GetUrl(PageTitle.Home); }
        }

        public static string AdminUser
        {
            get { return GetUrl(PageTitle.Users); }
        }

        public static string AdminHome
        {
            get { return GetUrl(PageTitle.Admin_Home); }
        }

        public static string AdminPoll
        {
            get { return GetUrl(PageTitle.Polls); }
        }

        public static string MarketplacePosts
        {
            get { return GetUrl(PageTitle.Marketplace_Posts); }
        }

        public static string AdminLogin
        {
            get { return GetUrl(PageTitle.Admin_Login); }
        }

        public static string MemberHome
        {
            get { return GetUrl(PageTitle.Member_Home); }
        }
        public static string MarketPlaceDetails
        {
            get { return GetUrl(PageTitle.MarketPlace_Details); }
        }

        public static string Register
        {
            get { return GetUrl(PageTitle.Register); }
        }

        public static string Login
        {
            get { return GetUrl(PageTitle.Login); }
        }

        public static string RegistrationConfirmation
        {
            get { return GetUrl(PageTitle.Registration_Confirmation); }
        }

        public static string RegistrationComplete
        {
            get { return GetUrl(PageTitle.Registration_Complete); }
        }

        public static string VerifyRegistration
        {
            get { return GetUrl(PageTitle.Verify_Registration); }
        }

        public static string ForgotPassword
        {
            get { return GetUrl(PageTitle.Forgot_Password); }
        }

        #endregion

        /// <summary>
        /// Takes a page title enumeration and returns 
        /// the url for the page that has the title in
        /// the sitemap file. PageTitle needs to be
        /// unique and there is a scope of perfomance
        /// tune for this method.
        /// </summary>
        /// <param name="pageTitle">Enumeration that represents title in the web.sitemap</param>
        /// <returns>The first url that has the title of the enumeration</returns>
        public static string GetUrl(PageTitle pageTitle)
        {
            StringDictionary urlDictornary = (StringDictionary)HttpContext.Current.Cache["SiteMap"];

            if (urlDictornary == null)
            {
                urlDictornary = new StringDictionary();
                SiteMapNodeCollection siteMapNodes = SiteMap.Provider.RootNode.GetAllNodes();

                if (siteMapNodes != null)
                {
                    urlDictornary.Add(SiteMap.Provider.RootNode.Title, SiteMap.Provider.RootNode.Url);

                    foreach (SiteMapNode node in siteMapNodes)
                    {
                        urlDictornary.Add(node.Title, node.Url);
                    }
                }
                HttpContext.Current.Cache.Insert("SiteMap",urlDictornary, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
            }

            return urlDictornary[pageTitle.ToString().Replace('_',' ')];
        }

        /// <summary>
        /// Unique enumeration for the page titles in the 
        /// web.sitemap file. It is recommanded not to put
        /// same title for multiple url
        /// </summary>
        public enum PageTitle
        {
            Home = 1,
            Admin_Home = 2,
            Polls = 3,
            Users = 4,
            Marketplace_Posts = 5,
            Admin_Login = 6,
            Member_Home = 7,
            Register = 8,
            Login = 9,
            Registration_Confirmation = 10,
            Registration_Complete = 11,
            Verify_Registration = 12,
            MarketPlace_Details=13,
            Forgot_Password = 14
        }
    }
}
